#!/bin/bash
# 如果没有输入任何参数,给出使用说明
if [[ $# != 6 ]];then
    echo "使用方法：$0 -w[仓数] -t[最大并发数] -m[分钟] -h[IP地址] -d[y/n] -c[P/M/O/G]"
    echo "例    如：$0 -w10 -t3 -m3 -h10.55.134.30  -dy -cM"
    exit 1;
fi

# ----------设置默认命令行参数---------
WAREHOUSES=1
MAXTERMINALS=1
RUN_MINS=1
IP="localhost"
INIT_DATA="no"
DB=""

# ----------获取命令行参数---------
while getopts :w:t:m:h:d:c: opt
do
    case "$opt" in
        w) 
            #echo "Got option '-w' with argument $OPTARG"
            WAREHOUSES=$OPTARG
            ;;
        t)
            #echo "Got option '-t' with argument $OPTARG"
            MAXTERMINALS=$OPTARG
            ;;
        m)
            #echo "Got option '-m' with argument $OPTARG"
            RUN_MINS=$OPTARG
            ;;
        h)
            #echo "Got option '-h' with argument $OPTARG"
            IP=$OPTARG
            ;;
        d)
            #echo "Got option '-d' with argument $OPTARG"
            if [[ "$OPTARG" = "y" ]];then
                INIT_DATA="yes"
            else
                INIT_DATA="no"
            fi
            ;;
        c)
            #echo "Got option '-c' with argument $OPTARG"
            DB=$OPTARG
            ;;            
        \?)
            echo "Invalid option: -$OPTARG" >&2
            exit 1
            ;;
    esac
done

# ----------MySQL参数---------
MDRIVER="com.mysql.cj.jdbc.Driver"
MCONN=jdbc:mysql://${IP}:3306/tpcc
#user=tpcc
#password=Test@1234

# ----------PG参数---------
PDRIVER="org.postgresql.Driver"
PCONN=jdbc:postgresql://${IP}:5432/tpcc
#user=tpcc
#password=Test@1234

# ----------GS参数---------
GDRIVER=org.opengauss.Driver
GCONN=jdbc:opengauss://${IP}:5432/tpcc
#user=tpcc
#password=Test@1234

# ----------Oracle参数---------
ODRIVER="oracle.jdbc.driver.OracleDriver"
OCONN=jdbc:oracle:thin:@${IP}:1521:orcl
#user=tpcc
#password=Test@1234

# 生成TPCC配置文件
function func_gen_prop() {
    local TERMINALS=$1
    echo "$(date "+%Y-%m-%d %H:%M:%S")**********************************开始生成配置文件:-w仓数 =${WAREHOUSES} -t并发数=${TERMINALS} -m分钟=${RUN_MINS} -d是否造数=${INIT_DATA}"
    if [[ "${DB}" = "P" ]];then
        prop_file="pg-${IP}.${TERMINALS}t"
        rm -rf ${prop_file}
        touch ${prop_file}

        echo "db=postgres
driver=${PDRIVER}
conn=${PCONN}
user=tpcc
password=Test@1234" >> ${prop_file}

    elif [[ "${DB}" = "M" ]];then
        prop_file="mysql-${IP}.${TERMINALS}t"
        rm -rf ${prop_file}
        touch ${prop_file}
        echo "db=mysql
driver=${MDRIVER}
conn=${MCONN}
user=tpcc
password=Test@1234" >> ${prop_file}

    elif [[ "${DB}" = "G" ]];then
        prop_file="gs-${IP}.${TERMINALS}t"
        rm -rf ${prop_file}
        touch ${prop_file}
        echo "db=openGauss
driver=${GDRIVER}
conn=${GCONN}
user=tpcc
password=Test@1234" >> ${prop_file}

    elif [[ "${DB}" = "O" ]];then
        prop_file="ora-${IP}.${TERMINALS}t"
        rm -rf ${prop_file}
        touch ${prop_file}
        echo "db=oracle
driver=${ODRIVER}
conn=${OCONN}
user=tpcc
password=Test1234" >> ${prop_file}
    else
        echo "Invalid DB Type!"
    fi

######################写入公共部分参数######################
    echo "
warehouses=${WAREHOUSES}
loadWorkers=8

terminals=${TERMINALS}
//To run specified transactions per terminal- runMins must equal zero
runTxnsPerTerminal=0
//To run for specified minutes- runTxnsPerTerminal must equal zero
runMins=${RUN_MINS}
//Number of total transactions per minute
limitTxnsPerMin=1000000000

//Set to true to run in 4.x compatible mode. Set to false to use the
//entire configured database evenly.
terminalWarehouseFixed=true

//The following five values must add up to 100
//The default percentages of 45, 43, 4, 4 & 4 match the TPC-C spec
newOrderWeight=45
paymentWeight=43
orderStatusWeight=4
deliveryWeight=4
stockLevelWeight=4
" >> ${prop_file}
}

function func_runData(){
  logfile=rundata${prop_file}.log
  echo "$(date "+%Y-%m-%d %H:%M:%S")**********************************开始造数runData ${WAREHOUSES} 仓 "
  rm -rf .jTPCC_run_seq.dat
  ./runDatabaseDestroy.sh ${prop_file}
  ./runDatabaseBuild.sh ${prop_file}
  rm -rf .jTPCC_run_seq.dat
}

function func_runBenchmark(){
  cur_date=$(date "+%Y%m%d")
  logfile=${cur_date}.${IP}.log
  rsfile=rs.${IP}.${cur_date}
  echo "$(date "+%Y-%m-%d %H:%M:%S")**********************************开始测试runBenchmark ${prop_file} " 
  ./runBenchmark.sh ${prop_file} >> ${logfile}

  rm -rf ${rsfile}
  cat ${logfile} | grep TPCC_RPT >> ${rsfile}
  tail -1 ${rsfile}
}

# 新增标志变量，用于确保 func_runData 只在最开始执行一次
DATA_INITIALIZED="no"

# 执行TPCC流程
function func_tpcc() {
  echo "------------------------------------------------------TPCC Start-----------------------------------------------"
  
  for ((i=4;i<=${MAXTERMINALS};i+=4))
  do
    #echo "************************************Running TPCC $i 并发"     
    func_gen_prop $i
	
	# 在最开始根据 INIT_DATA 的值决定是否执行 func_runData
    if [[ "${INIT_DATA}" = "yes" ]] && [[ "${DATA_INITIALIZED}" = "no" ]]; then
        func_runData
        DATA_INITIALIZED="yes"
    fi
    
    func_runBenchmark
    
    if [ $i -lt ${MAXTERMINALS} ] 
    then
        echo "Sleeping....." 
        sleep 30
    fi
  done
     
  echo "------------------------------------------------------TPCC Finished---------------------------------------------"
}

func_tpcc